package com.dino.manager.clients;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.dino.manager.AbstractPage;
import com.test.clients.dto.Client;
import com.test.clients.service.ClientsService;
import com.test.dao.SearchCriteria;
import com.test.dao.SearchResultSet;

public class ClientSearch extends AbstractPage {

	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = 5048532231293360785L;

	@SpringBean
	private ClientsService clientsService;

	public ClientSearch() {
		final SearchCriteria criteria = new SearchCriteria();
		criteria.setItemsPerPage(0);
		final SearchResultSet<Client> clients = clientsService.searchForClients(criteria);
		final List<Client> clientList = new ArrayList<Client>(clients.getResultList());
		final ListView<Client> rows = new ListView<Client>("searchResultRow", clientList) {

			/**
			 * Generated serial version UID.
			 */
			private static final long serialVersionUID = 8498648148021595960L;

			@Override
			protected void populateItem(final ListItem<Client> item) {
				final Client client = item.getModelObject();
				item.add(new ClientEditLink("clientLink", client.getId()));
				item.add(new Label("nameCol", client.getName()));
			}
		};

		add(rows);
	}

	private class ClientEditLink extends Link<EditClient> {

		/**
		 * Generated serial version UID.
		 */
		private static final long serialVersionUID = 3144825461230432635L;

		private final int clientId;

		public ClientEditLink(final String id, final int clientId) {
			super(id);
			this.clientId = clientId;
			add(new Label("idCol", clientId + ""));
		}

		@Override
		public void onClick() {
			setResponsePage(new EditClient(clientId));
		}
		
	}
}
